/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package action;

import java.io.*;

import javax.servlet.*;
import javax.servlet.http.*;
import negocio.UsuarioManager;
import persistencia.pojo.Usuario;

/**
 *
 * @author Victor
 */
public class ValidaUsuarioAction extends HttpServlet {
   
    /** 
    * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    * @param request servlet request
    * @param response servlet response
    */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String usuario = request.getParameter("txf_usuario");
        String clave = request.getParameter("txf_clave");  
        
        try {
            UsuarioManager mgr = new UsuarioManager();
            Usuario user = null;
            user = (Usuario) mgr.getUsuario(usuario);            
            
            if (user==null){
                mensaje(response,"Usuario no existe");
            }else{
                if(user.getClave().trim().equals(clave.trim())){
                   HttpSession session = request.getSession(true);
                   session.setAttribute("usuario", user);
                   response.sendRedirect("home.jsp");			
                } else{
                    mensaje(response,"Clave no es correcta");
                }
		
            }                        
        
        }catch (Exception e) {
            e.printStackTrace();
        }
    } 
    
    private void mensaje(HttpServletResponse response,String mensa) throws ServletException, IOException{
        PrintWriter out = response.getWriter();
        try {

            out.println("<html>");
            out.println("<head>");
            out.println("<title>error</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>ERROR: " + mensa + "</h1>");
            out.println("<p><a href='index.jsp'>Login</a></p>");                    
            out.println("</body>");
            out.println("</html>");

        } finally { 
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
    * Handles the HTTP <code>GET</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
    * Handles the HTTP <code>POST</code> method.
    * @param request servlet request
    * @param response servlet response
    */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
    * Returns a short description of the servlet.
    */
    public String getServletInfo() {
        return "Short description";
    }
    // </editor-fold>
}
